// ignore_for_file: must_be_immutable, unused_local_variable

import 'dart:convert';
import 'dart:typed_data';
import 'package:eh_mobile/common/utils/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../controller.dart';

class CenterCardWidget extends GetView<HomepageController> {
  CenterCardWidget({Key? key}) : super(key: key);

  late double _superHeight = 0;
  late double _superWidth = 0;

  // 主视图
  Widget _buildView() {
    final double cardWidth = (_superWidth - 16.r - 12.r) / 3.0;
    final double cardHeight = (_superHeight - 16.r - 6.r) / 2.0;
    final widgetList = <Widget>[];
    final ratioList = <int>[3, 2, 1];

    for (var i = 0; i < ratioList.length; i++) {
      int element = ratioList[i];
      Widget widget = Container(
              color: const Color.fromRGBO(255, 255, 255, 0.08),
              width: element * cardWidth + 6.r * (element - 1),
              height: cardHeight)
          .onClick(() {
        controller.centerCardAction(i);
      }).radius(radius: 14);
      widgetList.add(widget);
    }

    return Container(
      padding: const EdgeInsets.all(8).r,
      // color: Colors.white,
      child: Wrap(
        spacing: 6.r,
        runSpacing: 6.r,
        alignment: WrapAlignment.center,
        direction: Axis.horizontal,
        children: widgetList,
      ),
    );
  }

  Uint8List encode(String s) {
    var encodedString = utf8.encode(s);
    var encodedLength = encodedString.length;
    var data = ByteData(encodedLength + 4);
    data.setUint32(0, encodedLength, Endian.big);
    var bytes = data.buffer.asUint8List();
    bytes.setRange(4, encodedLength + 4, encodedString);
    return bytes;
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, constraints) {
      _superHeight = constraints.biggest.height; // 获取组件在父组件所能设置的最大高度
      _superWidth = constraints.biggest.width;

      return GetBuilder<HomepageController>(
        init: HomepageController(),
        id: "center_card_widget",
        builder: (_) {
          return Container(
              clipBehavior: Clip.antiAlias,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(const Radius.circular(22.0).r),
              ),
              child: Container(
                // color: const Color.fromRGBO(0, 0, 0, 0.38),
                child: _buildView(),
              ));
        },
      );
    });
  }
}
